wx.cloud.init()
var db = wx.cloud.database({
  env: "zousonglin1992-n4jsc"
})
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    code: 0,
    vip: "普通会员",
    phone: true
  },
  information: function() { //vip会员

  },
  authorize: function() { //判断是否授权
    var that = this
    wx.getSetting({
      success(res) {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(res) {
              that.setData({
                userInfo: res.userInfo
              })
            }
          })
        } else {
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }
      }
    })
  },
  callPhone() { //联系我们
    wx.showActionSheet({
      itemList: ["拨打手机15882354529", "拨打手机15928909202", "拨打座机(028) 6241 0827"],
      success(res) {
        console.log(res.tapIndex)
        if(res.tapIndex === 0){
          wx.makePhoneCall({
            phoneNumber: "15882354529"
          })
        } else if(res.tapIndex === 1){
          wx.makePhoneCall({
            phoneNumber: "15928909202"
          })
        } else if(res.tapIndex === 2){
          wx.makePhoneCall({
            phoneNumber: "02862410827"
          })
        }
      }
    })
  },
  getPhone() { //判断用户是否手机号码登录
    var that = this
    console.log("初始加载的phone", that.data.phone)
    wx.getStorage({
      key: 'phone',
      success: function(res) {
        console.log("获取到缓存的phone", res.data)
        that.setData({
          phone: res.data
        })
        console.log("缓存的phone放到data里面的phone", that.data.phone)
        // wx.clearStorage()
      }
    })
  },
  bindPhone: function() { //手机号码登录
    wx.navigateTo({
      url: '/pages/loginPhone/loginPhone'
    })
  },
  backHome: function() { //回到主页
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  getUserVipInfo() {
    var that = this
    wx.cloud.callFunction({
      name: "getUserVipInfo"
    }).then(res => {
      console.log("用户的Vip信息", res)
      that.setData({
        code: res.result.data[0].客户vip积分,
        vip: res.result.data[0].客户vip信息
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.getPhone()
    this.authorize()
    this.getUserVipInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getPhone()
    this.authorize()
    this.getUserVipInfo()
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#488fec'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.getPhone()
    this.getUserVipInfo()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})